package com.ld.security.server.admin.auth;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import com.ld.security.auth.common.sys.entity.SysUserEntity;
import com.ld.security.auth.common.sys.po.LoginPo;
import com.ld.security.auth.common.sys.po.UpdatePasswordPo;
import com.ld.security.auth.common.sys.service.ISysUserService;
import com.ld.security.auth.common.sys.vo.TokenVo;
import com.ld.security.auth.common.sys.vo.UserDetailsVo;
import com.ld.security.auth.shiro.util.LoginUtil;
import com.ld.security.common.core.exception.ApiException;
import com.ld.security.common.core.util.CrptUtil;
import com.ld.security.common.mybatis.base.vo.DataVo;
import com.ld.security.common.tomcat.anno.AjaxLogKey;
import com.ld.security.common.tomcat.util.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;

@RestController
@RequestMapping("/auth/authUser")
@Api(tags="用户授权中心")
public class AuthUserApi {



    @Autowired
    ISysUserService iSysUserService;


    String loginUrl="http://127.0.0.1:6080/oauth/token";

    private static String captchaKey="captcha";


    @RequestMapping(value="/createCode",method=RequestMethod.GET)
    @ApiOperation("创建验证码")
    @SneakyThrows
    @AjaxLogKey
    public void createCode() {
        ShearCaptcha captcha = SessionUtil.get(captchaKey);
        if(captcha == null) {
            captcha= CaptchaUtil.createShearCaptcha(200, 100, 4, 4);
            SessionUtil.set(captchaKey, captcha);
        }
        captcha.createCode();
        ServletOutputStream out = SessionUtil.getResponse().getOutputStream();
        captcha.write(out);
        out.close();
    }


    @AjaxLogKey
    @RequestMapping(value = "/loginByUsername",method = RequestMethod.POST)

    public DataVo<TokenVo> loginByUsername(@Validated @RequestBody LoginPo po) {


        String token =         LoginUtil.login(po);
        TokenVo vo = new TokenVo(token);
        return DataVo.of(vo);
    }



//    @Autowired
//    protected SimpMessageSendingOperations simpMessageSendingOperations;
//
//    @RequestMapping(value="/qrCodeCreate",method=RequestMethod.GET)
//    @ApiOperation("创建登录二维码")
//    @SneakyThrows
//
//    public void createQrCode(HttpServletResponse response, String token){
//        QrConfig config = new QrConfig(300, 300);
//// 设置边距，既二维码和背景之间的边距
//        // config.setMargin(3);
//// 设置前景色，既二维码颜色（青色）
//        //config.setForeColor(Color.CYAN.getRGB());
//// 设置背景色（灰色）
//        //  config.setBackColor(Color.GRAY.getRGB());
//
//// 生成二维码到文件，也可以到流
//
//        QrCodeUtil.generate("http://192.168.43.144:9527/#/msgLogin?token="+token, config,"png",response.getOutputStream());
//    }

    @AjaxLogKey
   @ApiOperation("注销")
    @RequestMapping(value="/logout",method=RequestMethod.POST)
    public DataVo logout() {
        LoginUtil.logout();
        DataVo vo = DataVo.of();
        vo.setMessage("注销成功");

       return vo;

   }
    @AjaxLogKey
    @ApiOperation("注册")
    @RequestMapping(value="/register",method=RequestMethod.POST)
    public DataVo register( @RequestBody LoginPo po){
        SysUserEntity userEntity =  new SysUserEntity();
        userEntity.setPassword(po.getPassword());
        userEntity.setUsername(po.getUsername());
        iSysUserService.register(userEntity);
        return DataVo.ofMsg("注册成功");
    }


    @AjaxLogKey
    @RequestMapping(value="/getUserInfo",method=RequestMethod.POST)
    @ApiOperation("获取当前用户详情")
    public DataVo getUserInfo(){
        UserDetailsVo vo =   LoginUtil.getUserDetails();
        return DataVo.of(vo);
    }

    @AjaxLogKey
    @RequestMapping(value="/updateUserInfo",method=RequestMethod.POST)
    @ApiOperation("更新当前用户详情")
    public DataVo updateUserInfo(@RequestBody SysUserEntity po){
        SysUserEntity e =  po;

        SysUserEntity userEntity =  LoginUtil.getUserInfo().getUser();

        userEntity.setLabel(e.getLabel());
        userEntity.setContent(e.getContent());
        userEntity.setLogo(po.getLogo());
        iSysUserService.save(userEntity);

        return DataVo.ofMsg("消息修改成功");
    }

    @AjaxLogKey
    @RequestMapping(value="/updatePassword",method=RequestMethod.POST)
    @ApiOperation("更新当前用户密码")
    public DataVo updatePassword(@RequestBody UpdatePasswordPo po){
        SysUserEntity user =  LoginUtil.getUserInfo().getUser();
        UpdatePasswordPo e =  po;
        CrptUtil.encrpt(e);
        if(!user.getPassword().equals(e.getOldPassword())){
            ApiException.throw400("密码错误，修改密码失败");
        }

        e.setUsername(user.getUsername());
        e.setId(user.getId());


        iSysUserService.updatePassword(e);
        return DataVo.ofMsg("密码修改成功");
}

    }